<%@page import="systems.AdsSystem"%>
<%@page import="helpers.StringHelper"%>
<%@page import="java.util.TreeMap"%>
<%@page import="entities.MetaSeo"%>
<%@page import="helpers.UrlHelper"%>
<%@page import="helpers.PaginatorHelper"%>
<%@page import="entities.Category"%>
<%@page import="entities.Author"%>
<%@page import="java.util.Map"%>
<%@page import="entities.Question"%>
<%@page import="java.util.List"%>
<%
List<Question> questions = (List<Question>) request.getAttribute("questions");
Map<String, Author> authors = (Map<String, Author>) request.getAttribute("authors");
Map<String, Category> categories = (Map<String, Category>) request.getAttribute("categories");
Category category = (Category) request.getAttribute("category");
Author author = null;

//Paginator
int pages = (Integer) request.getAttribute("page");
boolean nextPage = (Boolean) request.getAttribute("nextPage");
boolean prevPage = (Boolean) request.getAttribute("prevPage");
PaginatorHelper paginator = (PaginatorHelper) request.getAttribute("paginator");
MetaSeo metaseo = (MetaSeo) request.getAttribute("metaseo");

//Set content for Seo
Map<String, String> replates = new TreeMap<String, String>();
replates.put("__SERVER_NAME__", UrlHelper.getDomainServer(request));
replates.put("__SERVER_URL__", UrlHelper.getServerUrl(request));
replates.put("__CATEGORY_ID__", category.getId());
replates.put("__CATEGORY_NAME__", category.getName());
replates.put("__CATEGORY_ALIAS__", category.getAlias());
replates.put("__PAGE__", Integer.toString(pages));


String title = StringHelper.replateContent(metaseo.getTitle(), replates);
String description = StringHelper.replateContent(metaseo.getDescription(), replates);
String keywords = StringHelper.replateContent(metaseo.getKeywords(), replates);
String url = StringHelper.replateContent(metaseo.getOgurl(), replates);
%>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<jsp:include page="/templates/wpanswer/layouts/header.jsp">
	<jsp:param name="title" value="<%=title %>" />
    <jsp:param name="description" value="<%=description %>" />
    <jsp:param name="keywords" value="<%=keywords %>" />
    <jsp:param name="ogtitle" value="<%=title %>" />
    <jsp:param name="ogurl" value="<%=url %>" />
    <jsp:param name="ogdescription" value="<%=description%>" />
</jsp:include>
		<div class="row">
			<!-- Content BEGIN -->
			<div class="span8 main-content">
				<div class="content">
					<h1 class="title">Category: <%=category.getName() %></h1>
					<div class="breadcrumbs">
						<p>
							<a href="/">Home</a> » <%=category.getName() %>
						</p>
					</div>
					<hr />
				</div>
			
				<% 
				for (int i = 0; i < questions.size(); i++) {
					author = authors.get(questions.get(i).getAuthor());
				%>
				<question>
					<div class="row-fluid">
						<div class="span2 questionnumber">
							<p>
								<a href="/question/<%=questions.get(i).getId()%>"><%=questions.get(i).getAnswers().size() %></a>
							</p>
						</div>
						<div class="span10 questionmain">
							<h2><a href="/question/<%=questions.get(i).getId()%>" title="<%=questions.get(i).getTitle()%>"><%=questions.get(i).getTitle() %></a>	</h2>
							<div class="questionmeta">
								<% if (category != null) { %>
								<span>
									<a rel="tag" href="/category/<%=category.getAlias() %>-<%=category.getId()%>"><%=category.getName() %></a><br>
								</span>
								<% } %>
								<% if (author != null) { %>
								<span>
									Asked by: 
										<a href="/author/<%=author.getAlias() %>-<%=author.getId() %>" title="<%=author.getName() %>"><%=author.getName() %></a>
								</span>
								<% } else if(!questions.get(i).getAuthorName().isEmpty()) { %>
									
								<% }%>
							</div>
						</div>
					</div>
				</question>
				<% } %>
				
				<div class="pagination">
					<%
					List<Integer> range = paginator.getRangePage();
					%>
					<% if (prevPage) { %>
					<a href="<%=paginator.getPrev()%>">«</a>
					<% } %>
					
					<%
					if (nextPage || prevPage) {
						for (int i = 0; i < range.size(); i++) {
							if (range.get(i).intValue() == pages) {
					%>
						<a class="active"><%=range.get(i).toString() %></a>
						<%  } else if (nextPage || (!nextPage && range.get(i).intValue() <= pages)) { %>
						<a href="<%=paginator.getPage(range.get(i).intValue()) %>"><%=range.get(i).toString() %></a>
						<%  } %>
					<%  }
					} %>
					<% if (nextPage) { %>
					<a href="<%=paginator.getNext()%>">»</a>
					<% } %>
				</div>
			</div>
			<!-- Content END -->
			
			<!-- Slidebar BEGIN -->
			<jsp:include page="/templates/wpanswer/elements/right.jsp" />
			<!-- Slidebar END -->
		</div>
		
<jsp:include page="/templates/wpanswer/layouts/footer.jsp" />